package com.westvalley.service.api.hr;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.result.JsonResult;
import com.westvalley.http.HttpXmlClient;
import com.westvalley.syn.address.prop.AddressProp;

public abstract class HrService {
	private String url;
	
	protected HrService(String key){
		this.url = AddressProp.me().getPropValue(key);
	}
	
	/**
	 * 执行接口
	 * @param params 接口参数集合
	 * @return
	 */
	protected String executeInterface(Map<String,String> params){
		return HttpXmlClient.post(this.url, params);
	}
	
	/**
	 * 获取结果
	 * @return
	 */
	public JsonResult getJsonResult(){
		JsonResult JsonResult = com.westvalley.common.result.JsonResult.me("未知错误");
		try{
			Map<String,String> params = initParams();
			JSONObject ob = JSON.parseObject(executeInterface(params));
			if(StringKit.equals(ob.getString("code"), "0")){
				JsonResult = com.westvalley.common.result.JsonResult.me(ob.getJSONObject("result"));
			}else{
				throw new RuntimeException(ob.getString("msg"));
			}
		}catch (Exception e) {
			JsonResult = com.westvalley.common.result.JsonResult.me(e);
		}
		return JsonResult;
	}
	
	/**
	 * 初始化接口需求传入参数
	 * @param params
	 */
	protected abstract Map<String,String> initParams();
}
